<template>
  <div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FormPanel
      ref="baseInfo"
      :list="initFormBase"
      :cols="2"
      :labelWidth="170"
      :isSubmitBtn="false"
      :formType="handleType"
    />
    <div class="dialogBottmView">
      <el-button @click="closeDialogTop()">返 回</el-button>
      <el-button @click="saveDialog()" :loading="saveLoading" type="primary">
        回收交接
      </el-button>
    </div>
  </div>
</template>
<script>
import {dictionary} from '@/utils/dictMixin';
import {putHandover} from '@/api/partsmanage/batteryTrace/batteryRecycle';
import {getBatteryPlant} from '@/api/partsmanage/batteryTrace/batteryPlant';
import {tyqylxArr} from "./constant";

export default {
  components: {},
  props: {
    handleType: {
      type: String,
      default: 'add'
    },
    pageType: {
      type: String,
      default: ''
    },
    value: {
      type: Object,
      default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      formData: {
        vin: '',
        vehicleId: '',
        batterySpecies: '',
      },
      initFormBase: this.initFormBaseFun(),
      saveLoading: false,
    };
  },
  mounted() {
    this.getLeavePlants()
    this.formData = Object.assign({}, this.value.data);
    this.formData.retiredPlantName = this.formData.dealerName;
    this.formData.retiredPlantCode = this.formData.recoverUnitCode;
    this.init();
  },
  computed: {
    culFormData() {
      return this.formData
    }
  },
  methods: {
    //修改时，初始化页面数据
    init() {
      //循环为表单赋值
      this.initFormBase.forEach(x => {
        if (this.culFormData[x.fieldName]) {
          x.initialValue = this.culFormData[x.fieldName];
        }
      });
    },
    getLeavePlants() {
      getBatteryPlant({type: '2', available: true}).then(res => {
        if (res.resultCode == 200) {
          this.plantList = res.data
          const itemList = res.data.map(i => {
            return {
              text: i.name,
              value: i.id
            }
          })
          this.initFormBase.find(i => i.fieldName === 'leavePlantId').itemList = itemList
        }
      })
    },
    //基本表单信息
    initFormBaseFun() {
      return [
        {
          type: 'SELECT',
          label: '去向厂商名称',
          fieldName: 'leavePlantId',
          placeholder: '请选择',
          initialValue: '',
          change: val => {
            this.changeQxcs(val);
          },
          itemList: [],
          rules: [
            {required: true, message: '请选择', trigger: 'change'},
          ]
        },
        {
          type: 'INPUT',
          label: '去向厂商统一社会信用代码',
          fieldName: 'leavePlantCode',
          placeholder: '',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '退役厂商名称',
          fieldName: 'retiredPlantName',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '退役厂商统一社会信用代码',
          fieldName: 'retiredPlantCode',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'DATE',
          label: '退役日期',
          fieldName: 'retiredDate',
          placeholder: '请输入',
          disabled: this.handleType !== 'add',
          initialValue: new Date(),
          format: 'yyyy-MM-dd',
          rules: [
            {required: true, trigger: 'change', message: '请选择退役日期'},
          ],
        },
        {
          type: 'SELECT',
          label: '退役企业类型',
          fieldName: 'retireUnitType',
          placeholder: '请选择',
          initialValue: '',
          itemList: tyqylxArr,
          rules: [
            {required: true, trigger: 'change', message: '请选择'},
          ],
        },
      ];
    },
    changeQxcs(val) {
      let code = '';
      if (val) {
        code = this.plantList.find(i => i.id === val).socialCreditCode
      }
      this.initFormBase.find(i => i.fieldName === 'leavePlantCode').initialValue = code
    },
    //保存
    async saveDialog() {
      this.saveLoading = true;
      let [err, data] = await this.$refs.baseInfo.GET_FORM_DATA();
      if (err) {
      } else {
        this.formData = Object.assign({}, this.formData, data);
        const formData = {
          ...this.culFormData
        }
        let res = await putHandover(formData);
        if (res.resultCode === 200) {
          this.$notify.success({title: '提示信息', message: '保存成功!'});
          // 重新加载
          this.closeDialog('refresh');
        }
      }
      this.saveLoading = false;
    },
    closeDialogTop(confirm) {
      if (confirm) {
        this.$confirm('确认关闭窗口吗？您所做的操作将不会被保存！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.closeDialog('closeDialog');
        })
      } else {
        this.closeDialog('closeDialog');
      }
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', type);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
